package com.diudiutechnology.www.yadiudiu.yadiudiu.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.LazyFragment;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.SharedPreferencesUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.view.CircleImageView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.ApplicationActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.ApplyForEntryStatusActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.BusinessCenterActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.CollectionActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.CreditRatingActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.CreditScoreActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.MessageActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.OrderDealActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.PersonalDataActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.RedPacketsActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.RentalHouseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.SettingActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.SignIntegralActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.TheBalanceOfMyActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MessageActivityModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.common.Callback;

import java.util.List;

/**
 * Created by Administrator on 2017/8/16.
 */

public class MineFragment extends LazyFragment implements View.OnClickListener, Callback.CommonCallback<String> {
    private Context context;
    private LinearLayout setting_layout;//设置
    private RelativeLayout rental_phouse_layout;//我的出租
    private RelativeLayout order_details_layout;//交易明细
    private LinearLayout personal_ic;//头像
    private TextView name;//名字
    private TextView phone;//电话
    private LinearLayout norealname;//实名
    private RelativeLayout personal_data_layout;//个人资料
    private RelativeLayout layout_taskcenter;//我的余额
    private RelativeLayout application_layout;//申请入住
    private TextView application_text;//申请入驻文字
    private RelativeLayout texttitle_right;//消息
    private RelativeLayout sign_integral_packets;//签到积分
    private RelativeLayout layout_red_packets;//红包
    private RelativeLayout credit_score_packets;//授信积分
    private RelativeLayout credit_rating_packets;//信用积分
    private RelativeLayout collection_layout;//收藏
    private CircleImageView personal_icon;//头像
    private TextView balance_money;//余额
    private TextView red_packets_money;//红包余额
    private TextView sign_integral;//签到积分
    private TextView credit_score;//授信积分
    private TextView credit_ratingy;//信用等级
    private TextView norealname_text;//实名1
    private TextView personal_data;//实名2
    private TextView gexing_wei;//消息
    private String pos = "1";
    private String Judge_interface = "0";//判断哪个接口   0、个人中心首页 1、消息
    private MyProgressDialog mDialog;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private boolean Judge = true;//判断是否为首次进入

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_mine);
        initView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    private void initView() {
        personal_icon = (CircleImageView) findViewById(R.id.personal_icon);//头像
        balance_money = (TextView) findViewById(R.id.balance_money);//余额
        red_packets_money = (TextView) findViewById(R.id.red_packets_money);//红包余额
        sign_integral = (TextView) findViewById(R.id.sign_integral);//签到积分
        credit_score = (TextView) findViewById(R.id.credit_score);//授信积分
        credit_ratingy = (TextView) findViewById(R.id.credit_ratingy);//信用等级
        norealname_text = (TextView) findViewById(R.id.norealname_text);//实名1
        personal_data = (TextView) findViewById(R.id.personal_data);//实名2
        gexing_wei = (TextView) findViewById(R.id.gexing_wei);//消息
        setting_layout = (LinearLayout) findViewById(R.id.setting_layout);//设置
        rental_phouse_layout = (RelativeLayout) findViewById(R.id.rental_phouse_layout);//我的出租
        order_details_layout = (RelativeLayout) findViewById(R.id.order_details_layout);//交易明细
        personal_ic = (LinearLayout) findViewById(R.id.personal_ic);//头像
        name = (TextView) findViewById(R.id.name);//名字
        phone = (TextView) findViewById(R.id.phone);//电话
        norealname = (LinearLayout) findViewById(R.id.norealname);//实名
        personal_data_layout = (RelativeLayout) findViewById(R.id.personal_data_layout);//个人资料
        layout_taskcenter = (RelativeLayout) findViewById(R.id.layout_taskcenter);//我的余额
        application_layout = (RelativeLayout) findViewById(R.id.application_layout);//申请入住
        application_text = (TextView) findViewById(R.id.application_text);//申请入住文字
        texttitle_right = (RelativeLayout) findViewById(R.id.texttitle_right);//消息
        sign_integral_packets = (RelativeLayout) findViewById(R.id.sign_integral_packets);//签到积分
        layout_red_packets = (RelativeLayout) findViewById(R.id.layout_red_packets);//红包
        credit_score_packets = (RelativeLayout) findViewById(R.id.credit_score_packets);//授信积分
        credit_rating_packets = (RelativeLayout) findViewById(R.id.credit_rating_packets);//信用积分
        collection_layout = (RelativeLayout) findViewById(R.id.collection_layout);//收藏
        setting_layout.setOnClickListener(this);
        rental_phouse_layout.setOnClickListener(this);
        order_details_layout.setOnClickListener(this);
        layout_taskcenter.setOnClickListener(this);
        application_layout.setOnClickListener(this);
        texttitle_right.setOnClickListener(this);
        sign_integral_packets.setOnClickListener(this);
        layout_red_packets.setOnClickListener(this);
        credit_score_packets.setOnClickListener(this);
        credit_rating_packets.setOnClickListener(this);
        collection_layout.setOnClickListener(this);
        personal_ic.setOnClickListener(clickListener);
        name.setOnClickListener(clickListener);
        phone.setOnClickListener(clickListener);
        norealname.setOnClickListener(clickListener);
        personal_data_layout.setOnClickListener(clickListener);
        options = ImgUtils.mImageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        processMine();
    }

    /**
     * 头像、名字、电话、实名、个人资料 点击
     */
    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClass(context, PersonalDataActivity.class);
            startActivity(intent);
        }
    };

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.setting_layout://设置
                intent.setClass(context, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.rental_phouse_layout://我的出租
                intent.setClass(context, RentalHouseActivity.class);
                startActivity(intent);
                break;
            case R.id.order_details_layout://交易明细
                intent.setClass(context, OrderDealActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_taskcenter://我的余额
                intent.setClass(context, TheBalanceOfMyActivity.class);
                startActivity(intent);
                break;
            case R.id.application_layout://申请入住
                if (pos.equals("1")) {
                    intent.setClass(context, ApplicationActivity.class);
                    intent.putExtra("pos", "0");
                } else if (pos.equals("3")) {//商家中心
                    intent.setClass(context, BusinessCenterActivity.class);
                } else {//申请状态
                    intent.setClass(context, ApplyForEntryStatusActivity.class);
                }
                startActivity(intent);
                break;
            case R.id.texttitle_right://消息
                intent.setClass(context, MessageActivity.class);
                startActivity(intent);

                break;
            case R.id.sign_integral_packets://签到
                intent.setClass(context, SignIntegralActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_red_packets://红包
                intent.setClass(context, RedPacketsActivity.class);
                startActivity(intent);
                break;
            case R.id.credit_score_packets://授信积分
                intent.setClass(context, CreditScoreActivity.class);
                startActivity(intent);
                break;
            case R.id.credit_rating_packets://信用积分
                intent.setClass(context, CreditRatingActivity.class);
                startActivity(intent);
                break;
            case R.id.collection_layout://收藏
                intent.setClass(context, CollectionActivity.class);
                startActivity(intent);
                break;

        }

    }

    //个人中心
    private void processMine() {
        if (Sundry.isOpenNetwork(context)) {
            if (Judge_interface.equals("0")) {
                //      mDialog =new MyProgressDialog(context);
            }
            Judge_interface = "0";
            //    processemptying();
            HttpClient.mine(this);

        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //消息中心
    private void processMessages() {
        Judge_interface = "1";
        HttpClient.Messages(this);
    }

    //清空数据
    public void processemptying() {
        Judge = true;
        personal_icon.setImageResource(R.mipmap.thedefaultavatar);

        name.setText(getResources().getString(R.string.name));
        phone.setText("");
        //余额
        balance_money.setText("0.00元");
        //红包余额
        red_packets_money.setText("0.00元");
        // 签到积分
        sign_integral.setText("0");
        // 授信积分
        credit_score.setText("0");
        // 信用等级
        credit_ratingy.setText("0");
        personal_data.setText("未实名");
        norealname_text.setText("未实名");
        application_text.setText("申请入驻");

    }

    //绑定数据
    private void processdata(List<MineFragmentModel> mine) {
        MineFragmentModel model = mine.get(0);
        if (Judge) {
            //头像
            imageLoader.displayImage(Constant.HTTP + model.getAvatar(), personal_icon, options);
            Judge = false;
        } else {
            if (!UserInfoUtils.readUserInfo(context).getAvatar().equals(model.getAvatar())) {
                //头像
                imageLoader.displayImage(Constant.HTTP + model.getAvatar(), personal_icon, options);
            }
        }
        //姓名
        if (!Sundry.getStinges(model.getNickname())) {
            name.setText(model.getNickname());
        } else {
            name.setText(getResources().getString(R.string.name));
        }
        //电话
        if (!Sundry.getStinges(model.getPhone())) {
            String string = Sundry.getphon(model.getPhone()).toString();
            phone.setText(string);
        }
        //余额
        balance_money.setText(model.getBalance() + "元");
        //红包余额
        red_packets_money.setText(model.getRedenvelope() + "元");
        // 签到积分
        sign_integral.setText(model.getPoints());
        // 授信积分
        credit_score.setText(model.getCreditscore());
        // 信用等级
        credit_ratingy.setText(model.getCreditrating());

        //实名
        if (!Sundry.getStinges(model.getRealname())) {
            personal_data.setText("实名");
            norealname_text.setText("实名");
        } else {
            personal_data.setText("未实名");
            norealname_text.setText("未实名");
        }
        pos = model.getToexamine();
        if (pos.equals("3")) {
            application_text.setText("商家中心");
        } else {
            application_text.setText("申请入驻");
        }
        UserInfoUtils.getInstans().writeUserInfo(context, mine.get(0));//缓存
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        Judge_interface = "0";
        String string = SharedPreferencesUtil.getString(SharedPreferencesUtil.Keys.TOKEN_KEYS, "");
        if (!string.equals("")) {
            processMine();
        }
    }

    @Override
    public void onSuccess(String result) {
        if (Judge_interface.equals("0")) {//个人中心首页接口
//            if (mDialog != null) {
//                mDialog.dismiss();
//            }
            System.out.println("stringmd5result" + result);
            List<RegistrationModel> getnote = JsonTools.getunderlying(result);
            if (getnote != null || getnote.size() > 0) {
                String status = getnote.get(0).getStatus();
                String getMsg = getnote.get(0).getMsg();
                if (status.equals("200")) {
                    String content = getnote.get(0).getContent();
                    List<MineFragmentModel> mine = JsonTools.getMine(content);
                    if (mine != null || mine.size() > 0) {

                        processdata(mine);
                    }
                    processMessages();
                } else if (status.equals("105")) {
                    Intent intent = new Intent(context, LoginActivity.class);
                    startActivity(intent);
                    ToastUtils.show(context, getMsg);
                } else {
                    ToastUtils.show(context, getMsg);
                }
            }

        } else {//消息
            List<RegistrationModel> getnote = JsonTools.getunderlying(result);
            if (getnote != null || getnote.size() > 0) {
                String status = getnote.get(0).getStatus();
                String getMsg = getnote.get(0).getMsg();
                if (status.equals("200")) {
                    String content = getnote.get(0).getContent();
                    List<MessageActivityModel> message = JsonTools.getMessage(content);
                    if (message != null || message.size() > 0) {
                        String mes = "";
                        for (int i = 0; i < message.size(); i++) {
                            if (mes.equals("")) {
                                mes = message.get(i).getMsgstatus();
                            } else {
                                mes = mes + "," + message.get(i).getMsgstatus();
                            }
                        }
                        if (mes.indexOf("2") != -1) {
                            gexing_wei.setVisibility(View.VISIBLE);
                        } else {
                            gexing_wei.setVisibility(View.INVISIBLE);
                        }
                    }
                } else {
                    gexing_wei.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (Judge_interface.equals("0")) {
//            if (mDialog!=null){
//                mDialog.dismiss();
//            }
            ToastUtils.Toastshow(context, getResources().getString(R.string.server));
        }

    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (Judge_interface.equals("0")) {
//            if (mDialog!=null){
//                mDialog.dismiss();
//            }
            ToastUtils.Toastshow(context, getResources().getString(R.string.server));
        }

    }

    @Override
    public void onFinished() {

    }

    //刷新信息
    public void processout() {
        if (context != null) {
            Judge_interface = "0";
            String string = SharedPreferencesUtil.getString(SharedPreferencesUtil.Keys.TOKEN_KEYS, "");
            if (!string.equals("")) {
                processMine();
            }
        }
    }
}
